/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.CustomerEntity;
import ejb.CustomerEntityFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mohan
 */
@WebServlet(name = "ListCustomers", urlPatterns = {"/ListCustomers"})
public class ListCustomers extends HttpServlet {
    
    @EJB
    private CustomerEntityFacade customerEntityFacade;

   
    
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String cusID = request.getParameter("cusID");
        
        response.setContentType("text/html;charset=UTF-8");
        AppOptions apo = new AppOptions();
        String out_msg ="";
        
        //To delete a customer
        if (cusID != null) 
        {
                Long customerID = Long.parseLong(cusID);
                if(apo.checkUserOrderValidity(customerID)){
                    try{
                        CustomerEntity e =  customerEntityFacade.find(customerID);
                        customerEntityFacade.remove(e);
                        response.setHeader("Refresh", "1");
                    }
                    
                    
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                    
                }
                
                else
                {
                    out_msg = "Cannot Delete the Customer! He/She is already assign for an Order";
                }
            
            
        }
        
        
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ListCustomers</title>");            
            out.println("</head>");
            out.println("<div align='center'>");
            out.println("<body bgcolor='#CCCCCC'>");
            out.println("<a href = 'Mainindex'>Main Window | </a>");
            out.println("<a href = 'AddCustomer'>Add new customer | </a>");
            out.println("<a href = 'ListCustomers'>View Customers | </a>");
            out.println("<a href = 'AddCustomerOrder'>Add new customer Order | </a>");
            out.println("<a href = 'ListCustomerOrders'>View Customers Orders</a>");
            out.println("<h1>Customer Details</h1>");
            out.println("<br/>");
            out.println("<h3>"+out_msg+"</h3>");
            List customer = customerEntityFacade.findAll();
            out.println("<table width='800' border='1'>");
            out.println("<tr>");
            out.println("<td><b></b></td>");
            out.println("<td><b></b></td>");
            out.println("<td><b>Customer ID</b></td>");
            out.println("<td><b>Customer Name</b></td>");
            out.println("<td><b>Address</b></td>");
            out.println("<td><b>Contact No</b></td>");
            out.println("</tr>");
                        
            for(Iterator it = customer.iterator(); it.hasNext();)
            {
                CustomerEntity elem = (CustomerEntity) it.next();
                //out.println("<b>" +elem.getName()+"</b><br/>");
                
                out.println("<tr>");
                out.println("<td><a href = 'AddCustomer?cusID="+elem.getCustomerid()+"'>Update</a></td>");
                out.println("<td><a href = 'ListCustomers?cusID="+elem.getCustomerid()+"'>Remove</a></td>");
                out.println("<td>"+elem.getCustomerid()+"</td>");
                out.println("<td>"+elem.getName()+"</td>");
                out.println("<td>"+elem.getAddress()+"</td>");
                out.println("<td>"+elem.getContactno()+"</td>");
                out.println("</tr>");
                
            }
            out.println("</table>");
            out.println("<br/>");
            out.println("<a href = 'AddCustomer'>Add new customer</a>");
            out.println("</body>");
            out.println("</div>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
